package com.ys.io.p4对象流;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * 描述:
 * 使用对象流来克隆对象
 *  使用对象流来克隆对象,可以把对象保存再内存流中,等需要时在将其取出
 * @author CodeYang
 * @version 2021/1/4 14:07
 */
public class p2使用对象流来克隆对象 {


    public static void main(String[] args) throws Exception {

        // 初始化对象

        OutObject outObject = new OutObject("ys", 18, new InnerObject("wangwu"));

        try(// 内存流
            ByteArrayOutputStream bos=  new ByteArrayOutputStream();
            // 对象写(内存写)
            ObjectOutputStream oos =  new ObjectOutputStream(bos)
        ){
                //1 : 将对象写入内存流
            oos.writeObject(outObject);

            try(
                    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
                 )
            {
                OutObject newObject = (OutObject) ois.readObject();
                newObject.setName("wangba1");
                newObject.setInnerObject(new InnerObject("新"));
                //原对象
                System.out.println("old:"+outObject);
                //新对象
                System.out.println("new:"+newObject);

                System.out.println("地址值old:"+outObject.hashCode()+"地址值:new"+newObject.hashCode());

            }

        }


    }
}
